﻿@model Proggy.Web.Cms.Core.Models.AddedDragAndDropItemRenderViewModel
@{  
    //
    var item = Model.Item;

    // start optional attributes
    var optId = string.Empty;
    var optNodeId = string.Empty;
    var optClass_ = string.Empty;
    var optClear = string.Empty;
    var optFloat = string.Empty;
    var optWidth = string.Empty;
    var optPartName = string.Empty;
    var optMargin = string.Empty;
    var optConfigData1 = string.Empty;
    var optConfigData2 = string.Empty;
    var optConfigData3 = string.Empty;
    
    //
    if (Model.RenderAsEditor)
    {
        // add to cache
        DragAndDropManager.AddOrOverwriteToAddedPartsCache(item);

        // set optional attributes
        optId = " id=\"" + item.UniqueId + "\" ";
        optNodeId = "data-nodeid=\"" + item.NodeId + "\" ";
        optClass_ = "class=\"dand-item\" ";
        optClear = "data-clear=\"" + item.Clear + "\" ";
        optFloat = "data-float=\"" + item.Float + "\" ";
        optWidth = "data-width=\"" + item.Width + "\" ";
        optMargin = "data-margin=\"" + item.Margin + "\" ";
        optPartName = "data-partname=\"" + item.PartName + "\" ";
        optConfigData1 = "data-configdata=\"";
        optConfigData2 = "__CONFIGDATA" + HttpUtility.HtmlEncode(item.ConfigData);
        optConfigData3 = "\"";
        
    }

    // get optional string
    var optionalAttrsString =
        optId +
        optNodeId +
        optClass_ +
        optClear +
        optFloat +
        optWidth +
        optMargin +
        optPartName;

    // start style
    var style = new System.Text.StringBuilder();


    // check
    if (!string.IsNullOrWhiteSpace(item.Clear + item.Float + item.Margin + item.Width))
    {
        // start
        style.Append(" style=\"");

        // check width
        if (!string.IsNullOrWhiteSpace(item.Width))
        {
            style.Append("width: ");
            style.Append(item.Width);
            style.Append(";");
        }

        // check margin
        if (!string.IsNullOrWhiteSpace(item.Margin))
        {
            style.Append("margin: ");
            style.Append(item.Margin);
            style.Append(";");
        }

        // check float
        if (!string.IsNullOrWhiteSpace(item.Float))
        {
            style.Append("float: ");
            style.Append(item.Float);
            style.Append(";");
        }

        // check clear
        if (!string.IsNullOrWhiteSpace(item.Clear))
        {
            style.Append("clear: ");
            style.Append(item.Clear);
            style.Append(";");
        }

        // end
        style.Append("\" ");
    }    
    
    
    // final
    var finalAttr = Html.Raw(style + optionalAttrsString);

    // begin render
    <div@(finalAttr)@(Html.Raw(optConfigData1))@(optConfigData2)@(Html.Raw(optConfigData3))>
        @{
            // get part
            var part = DragAndDropManager.DragAndDropPartsCache.Single(x => x.Name == item.PartName);

            // get strongly typed
            var typedPart = (IAutoRenderPropertyType)Newtonsoft.Json.JsonConvert.DeserializeObject(item.ConfigData, part.PropertyType);

            //
            if (Model.RenderAsEditor)
            {            
            <div class="dand-item-header">
                <div class="dand-item-header-links">
                    <span>Item @Model.Number: </span><a href="#" class="dand-back-link">-</a> | <a href="#" class="dand-forward-link">
                        +</a> | @Html.ActionLink("Edit", "Edit", new { controller = "DragAndDrop", area = "ProggyCore", nodeId = item.NodeId, tempId = item.UniqueId, part = item.PartName }, new { @class = "dand-edit-link" })
                    | @Html.ActionLink("Delete", "DeleteFromCache", new { controller = "DragAndDrop", area = "ProggyCore" }, new { @class = "dand-delete-link", data_tempid = item.UniqueId })
                </div>
            </div>
            
            }
            <div class="dand-item-part">
                @{
                    // render
                    Html.AutoRenderPropertyTypePartial(typedPart);
                }
            </div>
        }
    </div> 
}
